package com.github.livebase.database;

import android.database.Cursor;

import com.github.livebase.util.base.ObjectsUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName OperatorUtil
 * @description:
 * @author: biao532
 * @create: 2024-01-24 11:22
 * @Version 1.0
 **/
public class OperatorUtil {

    protected static <T> T first(Cursor cursor, boolean move2First, ObjectsUtil.Converter<Cursor, T> converter) {
        if (cursor.getCount() <= 0) {
            return null;
        }
        if (move2First) {
            cursor.moveToFirst();
        }
        return converter.convert(cursor);
    }
    protected static  <T> List<T> cursorForeach(Cursor cursor, boolean move2First, ObjectsUtil.Converter<Cursor, T> converter) {
        if (cursor.getCount() <= 0) {
            return new ArrayList<>(0);
        }
        if (move2First) {
            cursor.moveToFirst();
        }
        List<T> list = new ArrayList<>(cursor.getCount() - cursor.getPosition());
        for (int i = cursor.getPosition(), l = cursor.getCount(); i < l; i++) {
            list.add(converter.convert(cursor));
            if (!cursor.moveToNext()) {
                break;
            };
        }
        return list;
    }
}
